package com.bestseller.poi.handler;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.bestseller.poi.service.ArrivalService;
import com.bestseller.poi.service.CTService;
import com.bestseller.poi.service.DinamondService;
import com.bestseller.poi.service.RepeatCampaignService;

/** 
* @author  xgx@bestseller.com.cn 
* @date 创建时间：2016年3月8日 下午2:46:13 
* @version 1.0  
*/
@Controller
public class FileUpload {
	@Autowired
	private DinamondService dinamondService;
	@Autowired
	private CTService cTService;
	@Autowired
	private RepeatCampaignService rcService;
	@Autowired
	private ArrivalService arrivalService;
	private static final Logger LOGGER=LoggerFactory.getLogger(FileUpload.class);
	//Diamond__StarShop表的数据导入
	@RequestMapping(value="/upload",method=RequestMethod.POST)
	public String upload(@RequestParam(value="file") MultipartFile file,@RequestParam(value="p4pName",required=false) String p4pName
			,@RequestParam(value="diamondName",required=false) String diamondName,
			@RequestParam(value="trafficName",required=false) String trafficName,Map<String,Object> map){
		LOGGER.info("导入ODS_sample_p4p表Excel文件工作表名sheet={},及 marketing_DIAMOND_STARSHOP表Excel文件工作表名sheet={}",p4pName, diamondName);
		CommonsMultipartFile cf= (CommonsMultipartFile)file; 
	    DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
	    File f = fi.getStoreLocation();
	    List<String> errors1=null;
	  //diamondStarShop和p4p两张表的上传
	    if(!p4pName.trim().equals("")||!diamondName.trim().equals("")){
	    	 		errors1 = dinamondService.upload(f,p4pName,diamondName);
					if(errors1!=null){
						if(errors1.size()>0){
							map.put("error",errors1);
						}
						return "success";
					}
	    }
	
	    //trafficSource和csStaff两张表的上传
	    List<String> errors2=null;
	    if(errors1==null){
				errors2 = cTService.upload(f,trafficName);
				if(errors2!=null){
					if(errors2.size()>0){
						map.put("error", errors2);
					}
					return "success";
				}
				
	    }
	    
	    
	    
	    //Campaign和repeat两张表的上传
	    List<String> errors3=null;
	    if(errors1==null&&errors2==null){
	    	errors3=rcService.upload(f);
	    	if(errors3!=null){
	    		if(errors3.size()>0){
		    		map.put("error", errors3);
		    	}
				return "success";
			}
	    }
	    
	    //ODS_New_Arrival_Stylecolor的上传
	    List<String> errors4=null;
	    if(errors1==null&&errors2==null&&errors3==null){
	    	
	    	errors4=arrivalService.upload(f);
	    	if(errors4!=null){
	    		if(errors4.size()>0){
		    		map.put("error", errors4);
		    	}
				return "success";
			}else{
				map.put("error","上传失败，请确保sheet工作名正确和存在！");
				return "success";
			}
	    }
	    
		return "null";
	}
	
	
}
